package com.yun.zhong.fu.sharecenter.rocketmq;


import com.yun.zhong.fu.sharecenter.domain.message.UserAddBonusMsgDTO;
import com.yun.zhong.fu.sharecenter.entity.Share;
import com.yun.zhong.fu.sharecenter.utils.SharedAuditDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * .
 * 简介说明: 消息生产者
 *
 * @author: RenPu
 * @date: 2025/04/17 11:39:52
 * @version: 1.0
 */

@Slf4j
@Component
@RequiredArgsConstructor(onConstructor =@__(@Autowired))
public class RocketMqProductor {

    //装配rockettemplate
    private final RocketMQTemplate rocketMQTemplate;


    public void sentAvailableMsg(Share share, Integer id, SharedAuditDto sharedAuditDto){
        String transcationId = UUID.randomUUID().toString();
        //向rocketMq发送半消息 （消息生产者）
        rocketMQTemplate.sendMessageInTransaction(
                "test-add-bonus",
                "add-bonus",
                MessageBuilder.withPayload(
                                UserAddBonusMsgDTO
                                        .builder()
                                        .bounus(500)
                                        .userId(share.getUserId())
                                        .build())
                        .setHeader(RocketMQHeaders.TRANSACTION_ID, transcationId)
                        .setHeader("share_id", id)
                        .build(),
                sharedAuditDto);
    }

}
